//
//  WFWebViewController.m
//  WisdomFarm
//
//  Created by sky on 2017/10/25.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFWebViewController.h"
#import <Masonry/Masonry.h>
#import "WFJSBridge.h"

@interface WFWebViewController ()<SCWebViewDelegate, WFJSBridgeExport>

@end

@implementation WFWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD showWithStatus:@"正在加载,请稍候..."];
    _webview = [[UIWebView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.webview];
    self.webview.scrollView.showsVerticalScrollIndicator = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.webview.backgroundColor = [UIColor whiteColor];
    self.webview.delegate = self;
    if (!IS_IPHONE_X) {
        if (IS_IOS_11) {
            self.webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }

    if (self.url) {
        NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
        [self.webview loadRequest:request];
    }
    
    [self.webview mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.insets(UIEdgeInsetsZero);
        make.bottom.offset(-44);
    }];
}

- (void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    [self.webview mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.insets(UIEdgeInsetsZero);
        make.bottom.offset(-self.view.safeAreaInsets.bottom);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+ (instancetype)controllerWithURL:(NSURL *)url {
    WFWebViewController *controller = [[WFWebViewController alloc] init];
    controller.url = url;
    return controller;
}

- (void)setUrl:(NSURL *)url {
    _url = url;
    if (self.isViewLoaded) {
        NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
        [self.webview loadRequest:request];
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([request.URL.absoluteString containsString:@"secondLevelPages/postAgriculture.html"]) {
        [WFRouter toAgricultureUpload];
        return NO;
    } else if ([request.URL.absoluteString containsString:@"secondLevelPages/postDisaster.html"]) {
        [WFRouter toDisasterUpload];
        return NO;
    }
    return YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"error:%@",error.localizedDescription);
    [SVProgressHUD showErrorWithStatus:@"加载失败"];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"load finished");
    [SVProgressHUD dismiss];
}

- (void)webView:(UIWebView *)webView didCreateJavaScriptContext:(JSContext *)ctx {
    [ctx setExceptionHandler:^(JSContext *context, JSValue *exception) {
        NSLog(@"%@",exception);
    }];
    __weak typeof(self) weakSelf = self;
    webView.sc_jsContext[@"JSBridge"] = weakSelf;
}

@end
